package com.haidong.controller;

import com.haidong.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    //设置当前响应方法的路径
    @RequestMapping("/save")
    // 该注解设置返回信息为json格式
    @ResponseBody
    public String save(){
        System.out.println("user save");
        return "{'info':'springmvc'}";
    }

    // 简单参数直接获取
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name,int age){
        System.out.println("接收到参数name:" + name + ",age:" + age);
        return "{'info':'commonParam'}";
    }

    // 前端传参名不同于后台
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    public String commonParamDifferentName(@RequestParam("name") String username, int age){
        // 参数和后台接受名字不同可以加注解匹配,还有节省框架性能特点
        System.out.println("接收到参数name:" + username + ",age:" + age);
        return "{'info':'commonParam'}";
    }

    // pojo传参要名称对应,支持嵌套
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println("pojo方法传递数据" + user);
        return "{'info':'commonParam'}";
    }

    // 数组形式的请求,只需要将请求参数设置为同一个键即可
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arraysParam(String[] likes){
        System.out.println("获取到数组参数" + Arrays.toString(likes));
        return "{'module':'array'}";
    }

    // 不能直接用集合当数组获取,要加@RequestParam获取
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes){
        System.out.println("集合形式获取参数,前端传入方式和数组一样" + likes);
        return "{'module':'list'}";
    }

    @RequestMapping("/listParamForJson")
    // ResponseBody注解的作用是将返回数据转化为json发送
    // 参数中的的RequestBody注解的作用是识别前端发来的json数据
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes){
        System.out.println("获取json传参" + likes);
        return "{'module':'listJson'}";
    }

    // pojo的json传参
    @RequestMapping("/pojoForJson")
    @ResponseBody
    public String pojoForJson(@RequestBody User user){
        System.out.println("获取json传参" + user);
        return "{'module':'pojoJson'}";
    }

    // list的json传参
    @RequestMapping("/listForJson")
    @ResponseBody
    public String listForJson(@RequestBody List<User> list){
        System.out.println("获取json传参" + list);
        return "{'module':'listJson'}";
    }

    // 如果前端传来的日期数据不是默认格式,需要加格式控制器
    @RequestMapping("/date")
    @ResponseBody
    public String date(LocalDate date,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date1,
                       @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") LocalDateTime date2){
        System.out.println(date);
        System.out.println(date1);
        System.out.println(date2);
        return "{'module':'time'}";
    }
}
